﻿using Microsoft.Phone.Controls;
using System.Windows.Controls;
using System;
using Microsoft.Phone.Shell;

namespace BootyShaker
{
    public partial class SettingsScreen : UserControl
    {
        Action returnMethod;
        public ApplicationBar ApplicationBar;
        ApplicationBarIconButton btnReset;
        public SettingsScreen(Action returnMeth)
        {
            InitializeComponent();
            returnMethod = returnMeth;
            CreateApplicationBar();

            slider_ValueChanged(Frequency, null);
            slider_ValueChanged(DampingRatio, null);
            slider_ValueChanged(GravityRatio, null);
          
            slider_ValueChanged(DragStrength, null);
            //slider_ValueChanged(LinearDamping, null);

        }
        public void GoBack()
        {
            Hide.Begin();
            returnMethod();
        }
        public void ShowPage(Action ret)
        {
            returnMethod = ret;
            Show.Begin();
        }
        void CreateApplicationBar()
        {
            ApplicationBar = new ApplicationBar();

            btnReset = new ApplicationBarIconButton();
            btnReset.IconUri = new Uri("/Icons/appbar.reset.rest.png", UriKind.Relative);
            btnReset.Text = "Defaults";
            btnReset.Click += btnDefaults_click;
            ApplicationBar.Buttons.Add(btnReset);

            //<shell:ApplicationBarIconButton IconUri="/Icons/appbar.reset.rest.png" Text="Defaults" Click="btnDefaults_click" />

        }

        private void btnSave_click(object sender, System.EventArgs e)
        {
            //appSettings.. = textBoxUsername.Text;
            //settings.PasswordSetting = passwordBoxPassword.Password;
            GoBack();
        }

        private void btnCancel_click(object sender, System.EventArgs e)
        {
            GoBack();
        }

        private void btnDefaults_click(object sender, System.EventArgs e)
        {

            Frequency.Value = AppSettings.FrequencyDefault;
            DampingRatio.Value = AppSettings.DampingRatioDefault;
            GravityRatio.Value = AppSettings.GravityRatioDefault;
                       DragStrength.Value = AppSettings.DragStrengthDefault;
           // LinearDamping.Value = AppSettings.LinearDampingDefault;
        }

        private void slider_ValueChanged(object sender, System.Windows.RoutedPropertyChangedEventArgs<double> e)
        {
            Slider s = (Slider)sender;
            if (s.Tag != null)
            {
                TextBlock lbl = ((System.Windows.Controls.TextBlock)(this.FindName((string)s.Tag)));
                if (lbl != null)
                {
                    if (lbl.Tag == null) { lbl.Tag = lbl.Text; }
                    lbl.Text = string.Format("{0} ({1:0.#})", (string)lbl.Tag, s.Value);
                }
            }
        }

        private void ToggleSwitch_Checked(object sender, System.Windows.RoutedEventArgs e)
        {
            MergeOverlay.Content = "Overlay";
        }

        private void ToggleSwitch_Unchecked(object sender, System.Windows.RoutedEventArgs e)
        {
            MergeOverlay.Content = "Merge";
        }

        private void Show_Completed(object sender, EventArgs e)
        {
            AppSettings set = new AppSettings();
            Frequency.Value = set.FrequencySetting;
            DampingRatio.Value = set.DampingRatioSetting;
            GravityRatio.Value = set.GravityRatioSetting;
            DragStrength.Value = set.DragStrengthSetting;
            MergeOverlay.IsChecked = set.MergeOverlaySetting;
        }

        private void btnBack_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            GoBack();
        }
    }
}